|
1
|
|
|
import { Inject } from '@nestjs/common'; |
|
2
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
|
3
|
|
|
import { IDateUtils } from 'src/Application/IDateUtils'; |
|
4
|
|
|
import { CanLeaveRequestBeModerated } from 'src/Domain/HumanResource/Leave/Specification/CanLeaveRequestBeModerated'; |
|
5
|
|
|
import { AcceptLeaveRequestCommand } from './AcceptLeaveRequestCommand'; |
|
6
|
|
|
import { IEventBus } from 'src/Application/IEventBus'; |
|
7
|
|
|
import { AcceptedLeaveRequestEvent } from '../Event/AcceptedLeaveRequestEvent'; |
|
8
|
|
|
import { DoesLeaveExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesLeaveExistForPeriod'; |
|
9
|
|
|
import { EventsOrLeavesAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsOrLeavesAlreadyExistForThisPeriodException'; |
|
10
|
|
|
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository'; |
|
11
|
|
|
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException'; |
|
12
|
|
|
import { LeaveRequestCantBeModeratedException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeModeratedException'; |
|
13
|
|
|
import { CreateNotificationCommand } from 'src/Application/Notification/Command/CreateNotificationCommand'; |
|
14
|
|
|
import { NotificationType } from 'src/Domain/Notification/Notification.entity'; |
|
15
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
|
16
|
|
|
import { ITranslator } from 'src/Infrastructure/Translations/ITranslator'; |
|
17
|
|
|
|
|
18
|
|
|
@CommandHandler(AcceptLeaveRequestCommand) |
|
19
|
|
|
export class AcceptLeaveRequestCommandHandler { |
|
20
|
|
|
constructor( |
|
21
|
|
|
@Inject('ILeaveRequestRepository') |
|
22
|
|
|
private readonly leaveRequestRepository: ILeaveRequestRepository, |
|
23
|
|
|
@Inject('IEventBus') |
|
24
|
|
|
private readonly eventBus: IEventBus, |
|
25
|
|
|
@Inject('IDateUtils') |
|
26
|
|
|
private readonly dateUtils: IDateUtils, |
|
27
|
|
|
private readonly canLeaveRequestBeModerated: CanLeaveRequestBeModerated, |
|
28
|
|
|
private readonly doesLeaveExistForPeriod: DoesLeaveExistForPeriod, |
|
29
|
|
|
@Inject('ICommandBus') |
|
30
|
|
|
private readonly commandBus: ICommandBus, |
|
31
|
|
|
@Inject('ITranslator') |
|
32
|
|
|
private readonly translator: ITranslator |
|
33
|
|
|
) {} |
|
34
|
|
|
|
|
35
|
|
|
public async execute(command: AcceptLeaveRequestCommand): Promise<string> { |
|
36
|
|
|
const { moderator, moderationComment, id } = command; |
|
37
|
|
|
|
|
38
|
|
|
const leaveRequest = await this.leaveRequestRepository.findOneById(id); |
|
39
|
|
|
if (!leaveRequest) { |
|
40
|
|
|
throw new LeaveRequestNotFoundException(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ( |
|
44
|
|
|
false === |
|
45
|
|
|
this.canLeaveRequestBeModerated.isSatisfiedBy(leaveRequest, moderator) |
|
46
|
|
|
) { |
|
47
|
|
|
throw new LeaveRequestCantBeModeratedException(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ( |
|
51
|
|
|
true === |
|
52
|
|
|
(await this.doesLeaveExistForPeriod.isSatisfiedBy( |
|
53
|
|
|
leaveRequest.getUser(), |
|
54
|
|
|
leaveRequest.getStartDate(), |
|
55
|
|
|
leaveRequest.getEndDate() |
|
56
|
|
|
)) |
|
57
|
|
|
) { |
|
58
|
|
|
throw new EventsOrLeavesAlreadyExistForThisPeriodException(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
leaveRequest.accept( |
|
62
|
|
|
moderator, |
|
63
|
|
|
this.dateUtils.getCurrentDateToISOString(), |
|
64
|
|
|
moderationComment |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
await this.leaveRequestRepository.save(leaveRequest); |
|
68
|
|
|
this.eventBus.publish(new AcceptedLeaveRequestEvent(leaveRequest)); |
|
69
|
|
|
|
|
70
|
|
|
this.commandBus.execute( |
|
71
|
|
|
new CreateNotificationCommand( |
|
72
|
|
|
NotificationType.REACTION, |
|
73
|
|
|
this.translator.translate( |
|
74
|
|
|
'leave-requests-approve-notification-emoji-name' |
|
75
|
|
|
), |
|
76
|
|
|
leaveRequest |
|
77
|
|
|
) |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
this.commandBus.execute( |
|
81
|
|
|
new CreateNotificationCommand( |
|
82
|
|
|
NotificationType.COMMENT, |
|
83
|
|
|
this.translator.translate( |
|
84
|
|
|
'leave-requests-approve-notification-message', |
|
85
|
|
|
{ |
|
86
|
|
|
moderatorFirstName: moderator.getFirstName() |
|
87
|
|
|
} |
|
88
|
|
|
), |
|
89
|
|
|
leaveRequest |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
return leaveRequest.getId(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|